package samber

import (
	"fmt"
	"github.com/samber/lo"
	"time"
)

// Buffer 对于管道拿指定个数组成Slice
func Buffer() {
	ch := make(chan int, 4)
	ch <- 1
	ch <- 2
	ch <- 3
	ch <- 5
	size := 3
	collection, length, readTime, ok := lo.Buffer[int](ch, size)

	fmt.Printf("%v %v %v %v \n", collection, length, readTime, ok) // [1 2 3] 3 1.647µs true
}

// BufferWithTimeout 相比Buffer较加了个超时
func BufferWithTimeout() {
	ch := make(chan int, 4)
	ch <- 1
	ch <- 2
	ch <- 3
	ch <- 5
	size := 3
	collection, length, readTime, ok := lo.BufferWithTimeout[int](ch, size, 1*time.Second)
	fmt.Printf("%v %v %v %v \n", collection, length, readTime, ok) // [1 2 3] 3 1.647µs true
}
